﻿namespace MauiApp2
{
    public partial class MainPage : ContentPage
    {
        public MainPage()
        {
            InitializeComponent();
        }

        string currentEntry = "";
        bool afterPoint = false;
        int inputState = 1;
        //1.wait num1;2.wait num2;3.recived num2;4.wait an operator
        string mathOperator;
        double firstNum, secondNum;
        void OnNum(object sender, EventArgs e)
        {
            Button button = (Button)sender;
            string pressed = button.Text;
            if (inputState == 4) 
            {
                Clear();
            }
            if ((this.Screen.Text == "0" )||(inputState==2))
            {
                this.Screen.Text = "";
                if (inputState == 2)
                    inputState = 3;
            }
            this.Screen.Text += pressed;
            currentEntry += pressed;
            //Refresh();
        }
        void OnPoint(object sender, EventArgs e)
        {
            Button button = (Button)sender;
            if (afterPoint == false)
            {
                afterPoint = true;
                if (this.Screen.Text == "0")
                {   currentEntry = "0."; }
                else 
                {   currentEntry += button.Text;}
                this.Screen.Text += button.Text;
            }
            //Refresh();
        }
        void OnOperater(object sender, EventArgs e)
        {
            if (inputState == 1 || inputState == 4)
            {
                if (inputState == 1)
                {
                    LockNumberValue(Screen.Text);
                }
                inputState = 2;
                afterPoint = false;
                Button button = (Button)sender;
                string pressed = button.Text;
                mathOperator = pressed;
                this.Screen.Text += button.Text;
            }
            //Refresh();
        }
        private void LockNumberValue(string text)
        {
            double number;
            if (double.TryParse(text, out number))
            {
                if (inputState == 1)
                {
                    firstNum = number;
                }
                else
                {
                    secondNum = number;
                }
                currentEntry = string.Empty;
            }
        }
        void OnClear(object sender, EventArgs e)
        {
            Clear();
        }
        void OnEqual(object sender, EventArgs e)
        {
            if (inputState == 3)
            {
                if (secondNum == 0)
                    LockNumberValue(Screen.Text);
                afterPoint = false;
                double result = Calculator.Calculate(firstNum, secondNum, mathOperator);
                this.Screen.Text = result.ToString();
                firstNum = result;
                secondNum = 0;
                inputState = 4;
                currentEntry = string.Empty;
            }
            //Refresh();
        }

        /*private void Refresh()
        {
            this.Screen2.Text = currentEntry;
        }
        */
        private void Clear()
        {
            this.Screen.Text = "0";
            currentEntry = "";
            afterPoint = false;
            inputState = 1;
           // Refresh();
        }
    }
    public static class Calculator
    {
        public static double Calculate(double value1, double value2, string mathOperator)
        {
            double result = 0;

            switch (mathOperator)
            {
                case "+":
                    result = value1 + value2;
                    break;
                case "-":
                    result = value1 - value2;
                    break;
                case "×":
                    result = value1 * value2;
                    break;
                case "÷":
                    result = value1 / value2;
                    break;
            }

            return result;
        }
    }
}
